package com.jws.jawestshow;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.android.jws.JwsIntents;
import com.jws.thirdlibrary.CxManager;

public class NFCActivity extends Activity {
    String TAG = "NFCActivity";
    private Button mButtonOpen;
    private Button mButtonClose;
    private Button mButtonWriteData;
    private TextView mCardID;
    private TextView mRawData;
    private String strCardID;
    private String strRawData;
    private CxManager mCxManager;
    private NfcIdReceiver mNfcIdReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nfc);
        //	mJwsHardwareManager = (JwsHardwareManager) getSystemService(Context.JWS_HARDWARE_SERVICE);
        mCardID = (TextView) findViewById(R.id.cardid);
        mCardID.setText("");
        mRawData = (TextView) findViewById(R.id.rawdata);
        mRawData.setText("");
        initButton();
        initData();
    }

    class NfcIdReceiver extends BroadcastReceiver {


        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.i(TAG, "onReceive: action " + action);
            if (action.equals(JwsIntents.REQUEST_RESPONSE_NFC_ID_ACTION)) {
                strCardID = intent.getStringExtra(JwsIntents.EXTRA_NFC_ID_RESPONSE);
                Log.i(TAG, "onReceive() " + "strCardID:  " + strCardID);
                mCardID.setText("Card id:" + strCardID);

                strRawData = intent.getStringExtra(JwsIntents.EXTRA_NFC_RAW_RESPONSE);
                Log.i(TAG, "onReceive() " + "strRawData:  " + strRawData);
                mRawData.setText("Raw data:" + strRawData);
            } else {
                Log.i(TAG, "onReceive: intent is null");
            }
        }
    }

    private void initButton() {
        mButtonOpen = (Button) findViewById(R.id.open);
        mButtonClose = (Button) findViewById(R.id.close);
        mButtonWriteData = (Button)findViewById(R.id.btn_write_data);
        mButtonOpen.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mCardID.setText("Card id:");
                mRawData.setText("Raw data:");
                registerNfc();
                Toast.makeText(NFCActivity.this, R.string.jws_open_success, Toast.LENGTH_SHORT).show();
            }
        });

        mButtonClose.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                unregisterNfc();
                mCardID.setText("");
                mRawData.setText("");
                Toast.makeText(NFCActivity.this, R.string.jws_close_success, Toast.LENGTH_SHORT).show();
            }
        });
        mButtonWriteData.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                byte[] buffer = new byte[]{0x11,0x22,0x33,0x44,0x55,0x66};
                int result = mCxManager.jwsSendCommandToNfc(buffer , buffer.length);
                Log.i(TAG, "write NFC data result : " + result );
            }
        });

    }

    private void initData() {
        mCxManager = CxManager.create(this);
    }

    public void registerNfc() {
        mNfcIdReceiver = new NfcIdReceiver();
        IntentFilter intentFilter = new IntentFilter(JwsIntents.REQUEST_RESPONSE_NFC_ID_ACTION);
        registerReceiver(mNfcIdReceiver, intentFilter);
        mCxManager = CxManager.create(this);
        mCxManager.jwsRegisterJwsHardwareNfcListener();
    }

    public void unregisterNfc() {
        mCxManager.jwsUnregisterJwsHardwareNfcListener();
        if (mNfcIdReceiver != null) {
            unregisterReceiver(mNfcIdReceiver);
            mNfcIdReceiver = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterNfc();
    }
}
